package com.dji.sample.wayline.controller;

import com.dji.sample.wayline.model.entity.Flyer;
import com.dji.sample.wayline.service.IFlyerService;
import com.dji.sdk.common.HttpResultResponse;
import com.dji.sdk.common.PaginationData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 飞手控制器
 * 提供飞手信息的RESTful API接口
 *
 * @author sean
 * @version 1.0
 * @date 2024/1/1
 */
@RestController
@RequestMapping("${url.wayline.prefix}${url.wayline.version}/flyers")
@Slf4j
public class FlyerController {

    @Autowired
    private IFlyerService flyerService;

    /**
     * 创建飞手信息
     * @param flyer 飞手信息
     * @return 创建结果
     */
    @PostMapping
    public HttpResultResponse<Flyer> createFlyer(@Valid @RequestBody Flyer flyer) {
        return flyerService.createFlyer(flyer);
    }

    /**
     * 根据ID删除飞手信息
     * @param id 飞手ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public HttpResultResponse<Boolean> deleteFlyerById(@PathVariable Long id) {
        return flyerService.deleteFlyerById(id);
    }

    /**
     * 批量删除飞手信息
     * @param ids 飞手ID列表
     * @return 删除结果
     */
    @DeleteMapping("/batch")
    public HttpResultResponse<String> deleteFlyers(@RequestBody List<Long> ids) {
        return flyerService.deleteFlyers(ids);
    }

    /**
     * 更新飞手信息
     * @param id 飞手ID
     * @param flyer 飞手信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public HttpResultResponse<Boolean> updateFlyer(@PathVariable Long id, @Valid @RequestBody Flyer flyer) {
        flyer.setId(id);
        return flyerService.updateFlyer(flyer);
    }

    /**
     * 根据ID查询飞手信息
     * @param id 飞手ID
     * @return 飞手信息
     */
    @GetMapping("/{id}")
    public HttpResultResponse<Flyer> getFlyerById(@PathVariable Long id) {
        Optional<Flyer> flyer = flyerService.getFlyerById(id);
        if (flyer.isPresent()) {
            return HttpResultResponse.success(flyer.get());
        } else {
            return HttpResultResponse.error("飞手不存在");
        }
    }

    /**
     * 根据飞手姓名查询飞手信息
     * @param flyerName 飞手姓名
     * @return 飞手信息
     */
    @GetMapping("/name/{flyerName}")
    public HttpResultResponse<Flyer> getFlyerByName(@PathVariable String flyerName) {
        Optional<Flyer> flyer = flyerService.getFlyerByName(flyerName);
        if (flyer.isPresent()) {
            return HttpResultResponse.success(flyer.get());
        } else {
            return HttpResultResponse.error("飞手不存在");
        }
    }

    /**
     * 根据手机号查询飞手信息
     * @param phone 手机号
     * @return 飞手信息
     */
    @GetMapping("/phone/{phone}")
    public HttpResultResponse<Flyer> getFlyerByPhone(@PathVariable String phone) {
        Optional<Flyer> flyer = flyerService.getFlyerByPhone(phone);
        if (flyer.isPresent()) {
            return HttpResultResponse.success(flyer.get());
        } else {
            return HttpResultResponse.error("飞手不存在");
        }
    }

    /**
     * 分页查询飞手信息
     * @param page 页码，默认为1
     * @param pageSize 每页大小，默认为10
     * @param status 飞手状态（可选）
     * @param level 飞手等级（可选）
     * @return 分页结果
     */
    @GetMapping
    public HttpResultResponse<PaginationData<Flyer>> getFlyers(
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(value = "page_size", defaultValue = "10") Long pageSize,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Integer level) {
        PaginationData<Flyer> result = flyerService.getFlyers(page, pageSize, status, level);
        return HttpResultResponse.success(result);
    }

    /**
     * 查询所有飞手信息
     * @return 飞手列表
     */
    @GetMapping("/all")
    public HttpResultResponse<List<Flyer>> getAllFlyers() {
        List<Flyer> flyers = flyerService.getAllFlyers();
        return HttpResultResponse.success(flyers);
    }

    /**
     * 根据状态查询飞手信息
     * @param status 飞手状态
     * @return 飞手列表
     */
    @GetMapping("/status/{status}")
    public HttpResultResponse<List<Flyer>> getFlyersByStatus(@PathVariable Integer status) {
        List<Flyer> flyers = flyerService.getFlyersByStatus(status);
        return HttpResultResponse.success(flyers);
    }

    /**
     * 根据等级查询飞手信息
     * @param level 飞手等级
     * @return 飞手列表
     */
    @GetMapping("/level/{level}")
    public HttpResultResponse<List<Flyer>> getFlyersByLevel(@PathVariable Integer level) {
        List<Flyer> flyers = flyerService.getFlyersByLevel(level);
        return HttpResultResponse.success(flyers);
    }
}